<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="前端,工程化">
    <meta name="description" content="前端工程化是使用软件工程的技术和方法来进行前端的开发流程、技术、工具、经验等规范化、标准化。">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>前端工程化 | 小沈曰</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
    
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/xiaoshensay/atom.xml" title="小沈曰" type="application/atom+xml">
<link rel="stylesheet" href="/xiaoshensay/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/xiaoshensay/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">小沈曰</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-list" style="zoom: 0.6;"></i>
      
      <span>Medias</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/xiaoshensay/musics">
          
          <i class="fas fa-music" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>Musics</span>
        </a>
      </li>
      
      <li>
        <a href="/xiaoshensay/movies">
          
          <i class="fas fa-film" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>Movies</span>
        </a>
      </li>
      
      <li>
        <a href="/xiaoshensay/books">
          
          <i class="fas fa-book" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>Books</span>
        </a>
      </li>
      
      <li>
        <a href="/xiaoshensay/galleries">
          
          <i class="fas fa-image" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>Galleries</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">小沈曰</div>
        <div class="logo-desc">
            
            计算机,读书,菜谱,旅游,宠物
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-list"></i>
			
			Medias
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="/xiaoshensay/musics " style="margin-left:75px";>
				  
				   <i class="fa fas fa-music" style="position: absolute;left:50px" ></i>
			      
		          <span>Musics</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/xiaoshensay/movies " style="margin-left:75px";>
				  
				   <i class="fa fas fa-film" style="position: absolute;left:50px" ></i>
			      
		          <span>Movies</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/xiaoshensay/books " style="margin-left:75px";>
				  
				   <i class="fa fas fa-book" style="position: absolute;left:50px" ></i>
			      
		          <span>Books</span>
                  </a>
                </li>
              
                <li>   
				
                  <a href="/xiaoshensay/galleries " style="margin-left:75px";>
				  
				   <i class="fa fas fa-image" style="position: absolute;left:50px" ></i>
			      
		          <span>Galleries</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/stszl" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/stszl" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '78c72f67941a420cd4e5ee9fdabcaeaba6d72f16160915085f9802220fd83799';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/xiaoshensay/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/2.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">前端工程化</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/xiaoshensay/tags/Vue/">
                                <span class="chip bg-color">Vue</span>
                            </a>
                        
                            <a href="/xiaoshensay/tags/webpack/">
                                <span class="chip bg-color">webpack</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/xiaoshensay/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA/" class="post-category">
                                计算机
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-06-15
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    16 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>

        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="1-模块化的分类"><a href="#1-模块化的分类" class="headerlink" title="1.模块化的分类"></a>1.模块化的分类</h2><h3 id="1-1-模块化概述"><a href="#1-1-模块化概述" class="headerlink" title="1.1 模块化概述"></a>1.1 模块化概述</h3><p>传统开发模式的主要问题<br>① 命名冲突<br>② 文件依赖</p>
<p>通过模块化解决上述问题</p>
<ul>
<li><strong>模块化</strong>就是把单独的一个功能封装到一个模块（文件）中，模块之间相互隔离，但是可以通过特定的接口公开内部成<br>员，也可以依赖别的模块</li>
<li>模块化开发的好处：方便代码的重用，从而提升开发效率，并且方便后期的维护</li>
</ul>
<h3 id="1-2-浏览器端的模块化"><a href="#1-2-浏览器端的模块化" class="headerlink" title="1.2 浏览器端的模块化"></a>1.2 浏览器端的模块化</h3><p>1).AMD(Asynchronous Module Definition,异步模块定义)<br>代表产品为：Require.js<br>2).CMD(Common Module Definition,通用模块定义)<br>代表产品为：Sea.js</p>
<h3 id="1-3-服务器端模块化规范"><a href="#1-3-服务器端模块化规范" class="headerlink" title="1.3 服务器端模块化规范"></a>1.3 服务器端模块化规范</h3><p>服务器端的模块化规范是使用CommonJS规范：<br>① 模块分为 <code>单文件模块</code> 与 <code>包</code>,一个文件就是一个模块，都拥有独立的作用域<br>② 模块成员导出： <code>module.exports</code> 和 <code>exports</code><br>③ 模块成员导入： <code>require('模块标识符')</code></p>
<h3 id="1-4-大一统的模块化规范-–-ES6模块化"><a href="#1-4-大一统的模块化规范-–-ES6模块化" class="headerlink" title="1.4 大一统的模块化规范 – ES6模块化"></a>1.4 大一统的模块化规范 – ES6模块化</h3><p>在 ES6 模块化规范诞生之前， Javascript 社区已经尝试并提出了 AMD、 CMD、 CommonJS 等模块化规范。</p>
<p>但是，这些社区提出的模块化标准，还是存在一定的差异性与局限性、 并不是浏览器与服务器通用的模块化标准，例如：</p>
<ul>
<li>AMD 和 CMD 适用于浏览器端的 Javascript 模块化</li>
<li>CommonJS 适用于服务器端的 Javascript 模块化<br>因此， ES6 语法规范中，在语言层面上定义了 ES6 模块化规范，是浏览器端与服务器端通用的模块化开发规范。</li>
</ul>
<p>ES6模块化规范中定义：</p>
<ul>
<li>每个js文件都是一个独立的模块</li>
<li>导入模块成员使用 import 关键字</li>
<li>暴露模块成员使用 export 关键字</li>
</ul>
<p><strong>小结</strong>：推荐使用ES6模块化，因为AMD，CMD局限使用与浏览器端，而CommonJS在服务器端使用。<br>        ES6模块化是浏览器端和服务器端通用的规范.</p>
<h3 id="1-5-Node-js-中通过-babel-体验-ES6-模块化"><a href="#1-5-Node-js-中通过-babel-体验-ES6-模块化" class="headerlink" title="1.5 Node.js 中通过 babel 体验 ES6 模块化"></a>1.5 Node.js 中通过 babel 体验 ES6 模块化</h3><p>① npm install –save-dev @babel/core @babel/cli @babel/preset-env @babel/node<br>② npm install –save @babel/polyfill<br>③ 项目跟目录创建文件 babel.config.js<br>④ babel.config.js 文件内容如右侧代码<br>⑤ 通过 npx babel-node index.js 执行代码</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">const</span> presets <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">[</span><span class="token string">"@babel/env"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    targets<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      edge<span class="token punctuation">:</span> <span class="token string">"17"</span><span class="token punctuation">,</span>
      firefox<span class="token punctuation">:</span> <span class="token string">"60"</span><span class="token punctuation">,</span>
      chrome<span class="token punctuation">:</span> <span class="token string">"67"</span><span class="token punctuation">,</span>
      safari<span class="token punctuation">:</span> <span class="token string">"11.1"</span>
    <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span> presets <span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre>
<h3 id="1-6-ES6-模块化的基本语法"><a href="#1-6-ES6-模块化的基本语法" class="headerlink" title="1.6 ES6 模块化的基本语法"></a>1.6 ES6 模块化的基本语法</h3><h4 id="1-6-1-默认导出-与-默认导入"><a href="#1-6-1-默认导出-与-默认导入" class="headerlink" title="1.6.1 默认导出 与 默认导入"></a>1.6.1 默认导出 与 默认导入</h4><ul>
<li>默认导出语法 export default 默认导出的成员</li>
<li>默认导入语法 import 接收名称 from ‘模块标识符’</li>
</ul>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 导入模块成员</span>
<span class="token keyword">import</span> m1 <span class="token keyword">from</span> <span class="token string">'./m1.js'</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m1<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// 打印输出的结果为：</span>
<span class="token comment" spellcheck="true">// { a: 10, c: 20, show: [Function: show] }</span></code></pre>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 当前文件模块为 m1.js</span>
<span class="token comment" spellcheck="true">// 定义私有成员 a 和 c</span>
<span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">let</span> c <span class="token operator">=</span> <span class="token number">20</span>
<span class="token comment" spellcheck="true">// 外界访问不到变量 d ,因为它没有被暴露出去</span>
<span class="token keyword">let</span> d <span class="token operator">=</span> <span class="token number">30</span>
<span class="token keyword">function</span> <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">// 将本模块中的私有成员暴露出去，供其它模块使用</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">,</span>
    c<span class="token punctuation">,</span>
    show
<span class="token punctuation">}</span></code></pre>
<p><strong>注意：每个模块中，只允许使用唯一的一次 export default，否则会报错！</strong></p>
<h4 id="1-6-2-按需导出-与-按需导入"><a href="#1-6-2-按需导出-与-按需导入" class="headerlink" title="1.6.2 按需导出 与 按需导入"></a>1.6.2 按需导出 与 按需导入</h4><ul>
<li>按需导出语法 export let s1 = 10</li>
<li>按需导入语法 import { s1 } from ‘模块标识符’</li>
</ul>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 导入模块成员</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> s1<span class="token punctuation">,</span> s2 <span class="token keyword">as</span> ss2<span class="token punctuation">,</span> say <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./m1.js'</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 打印输出 aaa</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ss2<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 打印输出 ccc</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>say<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 打印输出 [Function: say]</span></code></pre>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 当前文件模块为 m1.js</span>
<span class="token comment" spellcheck="true">// 向外按需导出变量 s1</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> s1 <span class="token operator">=</span> <span class="token string">'aaa'</span>
<span class="token comment" spellcheck="true">// 向外按需导出变量 s2</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> s2 <span class="token operator">=</span> <span class="token string">'ccc'</span>
<span class="token comment" spellcheck="true">// 向外按需导出方法 say</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> say <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre>
<h4 id="1-6-3-直接导入并执行模块代码"><a href="#1-6-3-直接导入并执行模块代码" class="headerlink" title="1.6.3 直接导入并执行模块代码"></a>1.6.3 直接导入并执行模块代码</h4><p>有时候，我们<code>只想单纯执行某个模块中的代码，并不需要得到模块中向外暴露的成员</code>，此时，可以直接导入并执行模块代码。</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 直接导入并执行模块代码</span>
<span class="token keyword">import</span> <span class="token string">'./m2.js'</span></code></pre>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 当前文件模块为 m2.js</span>
<span class="token comment" spellcheck="true">// 在当前模块中执行一个 for 循环操作</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="2-webpack"><a href="#2-webpack" class="headerlink" title="2. webpack"></a>2. webpack</h2><h3 id="2-1-当前-Web-开发面临的困境"><a href="#2-1-当前-Web-开发面临的困境" class="headerlink" title="2.1 当前 Web 开发面临的困境"></a>2.1 当前 Web 开发面临的困境</h3><ul>
<li>文件依赖关系错综复杂</li>
<li>静态资源请求效率低</li>
<li>模块化支持不友好</li>
<li>浏览器对高级 Javascript 特性兼容程度较低</li>
</ul>
<h3 id="2-2-webpack-概述"><a href="#2-2-webpack-概述" class="headerlink" title="2.2 webpack 概述"></a>2.2 webpack 概述</h3><p><code>webpack</code> 是一个流行的<code>前端项目构建工具（打包工具）</code> ，可以解决当前 web 开发中所面临的困境。<br>webpack 提供了<code>友好的模块化支持</code>，以及<code>代码压缩混淆、 处理 js 兼容问题、 性能优化</code>等强大的功能，从而让程序员把<br>工作的重心放到具体的功能实现上，提高了开发效率和项目的可维护性。</p>
<p>目前绝大多数企业中的前端项目，都是基于 webpack 进行打包构建的。<br><img width="80%" src="./images/01.png"></p>
<h3 id="2-3-webpack-的基本使用"><a href="#2-3-webpack-的基本使用" class="headerlink" title="2.3 webpack 的基本使用"></a>2.3 webpack 的基本使用</h3><h4 id="2-3-1-创建列表隔行变色项目"><a href="#2-3-1-创建列表隔行变色项目" class="headerlink" title="2.3.1 创建列表隔行变色项目"></a>2.3.1 创建列表隔行变色项目</h4><p>① 新建项目空白目录，并运行 npm init –y 命令，初始化包管理配置文件 package.json<br>② 新建 src 源代码目录<br>③ 新建 src -&gt; index.html 首页<br>④ 初始化首页基本的结构<br>⑤ 运行 npm install jquery –S 命令，安装 jQuery<br>⑥ 通过模块化的形式，实现列表隔行变色效果</p>
<h4 id="2-3-2-在项目中安装和配置-webpack"><a href="#2-3-2-在项目中安装和配置-webpack" class="headerlink" title="2.3.2 在项目中安装和配置 webpack"></a>2.3.2 在项目中安装和配置 webpack</h4><p>① 运行 npm install webpack webpack-cli –D 命令，安装 webpack 相关的包<br>② 在项目根目录中，创建名为 webpack.config.js 的 webpack 配置文件<br>③ 在 webpack 的配置文件中，初始化如下基本配置：</p>
<pre class=" language-javascript"><code class="language-javascript">module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    mode<span class="token punctuation">:</span> <span class="token string">'development'</span> <span class="token comment" spellcheck="true">// mode 用来指定构建模式</span>
<span class="token punctuation">}</span></code></pre>
<p>④ 在 package.json 配置文件中的 scripts 节点下，新增 dev 脚本如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token string">"scripts"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token string">"dev"</span><span class="token punctuation">:</span> <span class="token string">"webpack"</span> <span class="token comment" spellcheck="true">// script 节点下的脚本，可以通过 npm run 执行</span>
<span class="token punctuation">}</span></code></pre>
<p>⑤ 在终端中运行 npm run dev 命令，启动 webpack 进行项目打包。</p>
<h4 id="2-3-3-配置打包的入口与出口"><a href="#2-3-3-配置打包的入口与出口" class="headerlink" title="2.3.3 配置打包的入口与出口"></a>2.3.3 配置打包的入口与出口</h4><p>webpack 的 4.x 版本中默认约定：</p>
<ul>
<li>打包的入口文件为 src -&gt; index.js</li>
<li>打包的输出文件为 dist -&gt; main.js</li>
</ul>
<p>如果要修改打包的入口与出口，可以在 webpack.config.js 中新增如下配置信息：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 导入 node.js 中专门操作路径的模块</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    entry<span class="token punctuation">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'./src/index.js'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// 打包入口文件的路径</span>
    output<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        path<span class="token punctuation">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'./dist'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// 输出文件的存放路径</span>
        filename<span class="token punctuation">:</span> <span class="token string">'bundle.js'</span> <span class="token comment" spellcheck="true">// 输出文件的名称</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<h4 id="2-3-4-配置-webpack-的自动打包功能"><a href="#2-3-4-配置-webpack-的自动打包功能" class="headerlink" title="2.3.4 配置 webpack 的自动打包功能"></a>2.3.4 配置 webpack 的自动打包功能</h4><p>① 运行 npm install webpack-dev-server –D 命令，安装支持项目自动打包的工具<br>② 修改 package.json -&gt; scripts 中的 dev 命令如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token string">"scripts"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token string">"dev"</span><span class="token punctuation">:</span> <span class="token string">"webpack-dev-server"</span> <span class="token comment" spellcheck="true">// script 节点下的脚本，可以通过 npm run 执行</span>
<span class="token punctuation">}</span></code></pre>
<p>③ 将 src -&gt; index.html 中， script 脚本的引用路径，修改为 “/buldle.js“<br>④ 运行 npm run dev 命令，重新进行打包<br>⑤ 在浏览器中访问 <a href="http://localhost:8080" target="_blank" rel="noopener">http://localhost:8080</a> 地址，查看自动打包效果</p>
<p><strong>注意：</strong></p>
<ul>
<li>webpack-dev-server 会启动一个实时打包的 http 服务器</li>
<li>webpack-dev-server 打包生成的输出文件，默认放到了项目根目录中，而且是虚拟的、看不见的</li>
</ul>
<h4 id="2-3-5-配置-html-webpack-plugin-生成预览页面"><a href="#2-3-5-配置-html-webpack-plugin-生成预览页面" class="headerlink" title="2.3.5 配置 html-webpack-plugin 生成预览页面"></a>2.3.5 配置 html-webpack-plugin 生成预览页面</h4><p>① 运行 npm install html-webpack-plugin –D 命令，安装生成预览页面的插件<br>② 修改 webpack.config.js 文件头部区域，添加如下配置信息：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 导入生成预览页面的插件，得到一个构造函数</span>
<span class="token keyword">const</span> HtmlWebpackPlugin <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'html-webpack-plugin'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> htmlPlugin <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HtmlWebpackPlugin</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 创建插件的实例对象</span>
    template<span class="token punctuation">:</span> <span class="token string">'./src/index.html'</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// 指定要用到的模板文件</span>
    filename<span class="token punctuation">:</span> <span class="token string">'index.html'</span> <span class="token comment" spellcheck="true">// 指定生成的文件的名称，该文件存在于内存中，在目录中不显示</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
<p>③ 修改 webpack.config.js 文件中向外暴露的配置对象，新增如下配置节点：</p>
<pre class=" language-javascript"><code class="language-javascript">module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span> htmlPlugin <span class="token punctuation">]</span> <span class="token comment" spellcheck="true">// plugins 数组是 webpack 打包期间会用到的一些插件列表</span>
<span class="token punctuation">}</span></code></pre>
<h4 id="2-3-6-配置自动打包相关的参数"><a href="#2-3-6-配置自动打包相关的参数" class="headerlink" title="2.3.6 配置自动打包相关的参数"></a>2.3.6 配置自动打包相关的参数</h4><pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// package.json中的配置</span>
<span class="token comment" spellcheck="true">// --open 打包完成后自动打开浏览器页面</span>
<span class="token comment" spellcheck="true">// --host 配置 IP 地址</span>
<span class="token comment" spellcheck="true">// --port 配置端口</span>
<span class="token string">"scripts"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token string">"dev"</span><span class="token punctuation">:</span> <span class="token string">"webpack-dev-server --open --host 127.0.0.1 --port 8888"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span></code></pre>
<h2 id="2-4-webpack-中的加载器"><a href="#2-4-webpack-中的加载器" class="headerlink" title="2.4 webpack 中的加载器"></a>2.4 webpack 中的加载器</h2><h3 id="2-4-1-通过-loader-打包非-js-模块"><a href="#2-4-1-通过-loader-打包非-js-模块" class="headerlink" title="2.4.1 通过 loader 打包非 js 模块"></a>2.4.1 通过 loader 打包非 js 模块</h3><p>在实际开发过程中， webpack 默认只能打包处理以 .js 后缀名结尾的模块，其他非 .js 后缀名结<br>尾的模块， webpack 默认处理不了， 需要调用 loader 加载器才可以正常打包，否则会报错！</p>
<p>loader 加载器可以协助 webpack 打包处理特定的文件模块，比如：</p>
<ul>
<li>less-loader 可以打包处理 .less 相关的文件</li>
<li>sass-loader 可以打包处理 .scss 相关的文件</li>
<li>url-loader 可以打包处理 css 中与 url 路径相关的文件</li>
</ul>
<h3 id="2-4-2-loader-的调用过程"><a href="#2-4-2-loader-的调用过程" class="headerlink" title="2.4.2 loader 的调用过程"></a>2.4.2 loader 的调用过程</h3><img width="80%" src="./images/02.png">

<h2 id="2-5-webpack-中加载器的基本使用"><a href="#2-5-webpack-中加载器的基本使用" class="headerlink" title="2.5 webpack 中加载器的基本使用"></a>2.5 webpack 中加载器的基本使用</h2><ul>
<li>CSS</li>
<li>LESS</li>
<li>SCSS</li>
<li>PostCSS</li>
<li>JavaScript</li>
<li>Image/Font</li>
<li>Vue</li>
</ul>
<img width="80%" src="./images/03.png">

<h3 id="2-5-1-打包处理-css-文件"><a href="#2-5-1-打包处理-css-文件" class="headerlink" title="2.5.1 打包处理 css 文件"></a>2.5.1 打包处理 css 文件</h3><p>① 运行 npm i style-loader css-loader -D 命令，安装处理 css 文件的 loader<br>② 在 webpack.config.js 的 module -&gt; rules 数组中，添加 loader 规则如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 所有第三方文件模块的匹配规则</span>
module<span class="token punctuation">:</span> <span class="token punctuation">{</span>
rules<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> 
    test<span class="token punctuation">:</span> <span class="token regex">/\.css$/</span><span class="token punctuation">,</span> use<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'style-loader'</span><span class="token punctuation">,</span> <span class="token string">'css-loader'</span><span class="token punctuation">]</span> 
    <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<p>其中， test 表示匹配的文件类型， use 表示对应要调用的 loader</p>
<p><strong>注意：</strong></p>
<ul>
<li>use 数组中指定的 loader 顺序是固定的</li>
<li>多个 loader 的调用顺序是：从后往前调用</li>
</ul>
<h3 id="2-5-2-打包处理-less-文件"><a href="#2-5-2-打包处理-less-文件" class="headerlink" title="2.5.2 打包处理 less 文件"></a>2.5.2 打包处理 less 文件</h3><p>① 运行 npm i less-loader less -D 命令<br>② 在 webpack.config.js 的 module -&gt; rules 数组中，添加 loader 规则如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 所有第三方文件模块的匹配规则</span>
module<span class="token punctuation">:</span> <span class="token punctuation">{</span>
rules<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
     test<span class="token punctuation">:</span> <span class="token regex">/\.less$/</span><span class="token punctuation">,</span> use<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'style-loader'</span><span class="token punctuation">,</span> <span class="token string">'css-loader'</span><span class="token punctuation">,</span> <span class="token string">'less-loader'</span><span class="token punctuation">]</span> 
     <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="2-5-3-打包处理-scss-文件"><a href="#2-5-3-打包处理-scss-文件" class="headerlink" title="2.5.3 打包处理 scss 文件"></a>2.5.3 打包处理 scss 文件</h3><p>① 运行 npm i sass-loader node-sass -D 命令<br>② 在 webpack.config.js 的 module -&gt; rules 数组中，添加 loader 规则如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 所有第三方文件模块的匹配规则</span>
module<span class="token punctuation">:</span> <span class="token punctuation">{</span>
rules<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> 
    test<span class="token punctuation">:</span> <span class="token regex">/\.scss$/</span><span class="token punctuation">,</span> use<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'style-loader'</span><span class="token punctuation">,</span> <span class="token string">'css-loader'</span><span class="token punctuation">,</span> <span class="token string">'sass-loader'</span><span class="token punctuation">]</span> 
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="2-5-4-配置-postCSS-自动添加-css-的兼容前缀"><a href="#2-5-4-配置-postCSS-自动添加-css-的兼容前缀" class="headerlink" title="2.5.4 配置 postCSS 自动添加 css 的兼容前缀"></a>2.5.4 配置 postCSS 自动添加 css 的兼容前缀</h3><p>① 运行 npm i postcss-loader autoprefixer -D 命令<br>② 在项目根目录中创建 postcss 的配置文件 postcss.config.js，并初始化如下配置：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">const</span> autoprefixer <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'autoprefixer'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 导入自动添加前缀的插件</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span> autoprefixer <span class="token punctuation">]</span> <span class="token comment" spellcheck="true">// 挂载插件</span>
<span class="token punctuation">}</span></code></pre>
<p>③ 在 webpack.config.js 的 module -&gt; rules 数组中，修改 css 的 loader 规则如下：</p>
<pre class=" language-javascript"><code class="language-javascript">module<span class="token punctuation">:</span> <span class="token punctuation">{</span>
rules<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> 
    test<span class="token punctuation">:</span><span class="token regex">/\.css$/</span><span class="token punctuation">,</span> use<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'style-loader'</span><span class="token punctuation">,</span> <span class="token string">'css-loader'</span><span class="token punctuation">,</span> <span class="token string">'postcss-loader'</span>
    <span class="token punctuation">]</span><span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="2-5-5-打包样式表中的图片和字体文件"><a href="#2-5-5-打包样式表中的图片和字体文件" class="headerlink" title="2.5.5 打包样式表中的图片和字体文件"></a>2.5.5 打包样式表中的图片和字体文件</h3><p>① 运行 npm i url-loader file-loader -D 命令<br>② 在 webpack.config.js 的 module -&gt; rules 数组中，添加 loader 规则如下:</p>
<pre class=" language-javascript"><code class="language-javascript">module<span class="token punctuation">:</span> <span class="token punctuation">{</span>
rules<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    test<span class="token punctuation">:</span> <span class="token regex">/\.jpg|png|gif|bmp|ttf|eot|svg|woff|woff2$/</span><span class="token punctuation">,</span>
    use<span class="token punctuation">:</span> <span class="token string">'url-loader?limit=16940'</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<p>其中 ? 之后的是 loader 的参数项。<br>limit 用来指定图片的大小，单位是字节(byte),只有小于 limit 大小的图片，才会被转为 base64 图片</p>
<h3 id="2-5-6-打包处理-js-文件中的高级语法"><a href="#2-5-6-打包处理-js-文件中的高级语法" class="headerlink" title="2.5.6. 打包处理 js 文件中的高级语法"></a>2.5.6. 打包处理 js 文件中的高级语法</h3><p>① 安装babel转换器相关的包： npm i babel-loader @babel/core @babel/runtime -D<br>② 安装babel语法插件相关的包： npm i @babel/preset-env @babel/plugin-transformruntime @babel/plugin-proposal-class-properties –D<br>③ 在项目根目录中，创建 babel 配置文件 babel.config.js 并初始化基本配置如下：</p>
<pre class=" language-javascript"><code class="language-javascript">module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    presets<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token string">'@babel/preset-env'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
    plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token string">'@babel/plugin-transform-runtime'</span><span class="token punctuation">,</span> '@babel<span class="token operator">/</span>plugin<span class="token operator">-</span>proposalclass<span class="token operator">-</span>properties’ <span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<p>④ 在 webpack.config.js 的 module -&gt; rules 数组中，添加 loader 规则如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// exclude 为排除项，表示 babel-loader 不需要处理 node_modules 中的 js 文件</span>
<span class="token punctuation">{</span> test<span class="token punctuation">:</span> <span class="token regex">/\.js$/</span><span class="token punctuation">,</span> use<span class="token punctuation">:</span> <span class="token string">'babel-loader'</span><span class="token punctuation">,</span> exclude<span class="token punctuation">:</span> <span class="token regex">/node_modules/</span> <span class="token punctuation">}</span></code></pre>
<h2 id="3-Vue-单文件组件"><a href="#3-Vue-单文件组件" class="headerlink" title="3. Vue 单文件组件"></a>3. Vue 单文件组件</h2><h3 id="3-1-传统组件的问题和解决方案"><a href="#3-1-传统组件的问题和解决方案" class="headerlink" title="3.1 传统组件的问题和解决方案"></a>3.1 传统组件的问题和解决方案</h3><h4 id="3-1-1-问题"><a href="#3-1-1-问题" class="headerlink" title="3.1.1 问题"></a>3.1.1 问题</h4><ol>
<li>全局定义的组件必须保证组件的名称不重复</li>
<li>字符串模板缺乏语法高亮，在 HTML 有多行的时候，需要用到丑陋的 \</li>
<li>不支持 CSS 意味着当 HTML 和 JavaScript 组件化时， CSS 明显被遗漏</li>
<li>没有构建步骤限制，只能使用 HTML 和 ES5 JavaScript, 而不能使用预处理器（如： Babel）</li>
</ol>
<h4 id="3-1-2-解决方案"><a href="#3-1-2-解决方案" class="headerlink" title="3.1.2 解决方案"></a>3.1.2 解决方案</h4><p>针对传统组件的问题， Vue 提供了一个解决方案 —— 使用 Vue 单文件组件。</p>
<h3 id="3-2-Vue单文件组件的基本用法"><a href="#3-2-Vue单文件组件的基本用法" class="headerlink" title="3.2 Vue单文件组件的基本用法"></a>3.2 Vue单文件组件的基本用法</h3><p>单文件组件的组成结构</p>
<ul>
<li>template 组件的模板区域</li>
<li>script 业务逻辑区域</li>
<li>style 样式区域</li>
</ul>
<pre class=" language-javascript"><code class="language-javascript"><span class="token operator">&lt;</span>template<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 这里用于定义Vue组件的模板内容 <span class="token operator">--</span><span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>template<span class="token operator">></span>
<span class="token operator">&lt;</span>script<span class="token operator">></span>
    <span class="token comment" spellcheck="true">// 这里用于定义Vue组件的业务逻辑</span>
    <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
        data<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// 私有数据</span>
        methods<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 处理函数</span>
        <span class="token comment" spellcheck="true">// ... 其它业务逻辑</span>
<span class="token punctuation">}</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span>
<span class="token operator">&lt;</span>style scoped<span class="token operator">></span>
    <span class="token comment" spellcheck="true">/* 这里用于定义组件的样式 */</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>style<span class="token operator">></span></code></pre>
<h3 id="3-3-webpack-中配置-vue-组件的加载器"><a href="#3-3-webpack-中配置-vue-组件的加载器" class="headerlink" title="3.3 webpack 中配置 vue 组件的加载器"></a>3.3 webpack 中配置 vue 组件的加载器</h3><p>① 运行 npm i vue-loader vue-template-compiler -D 命令<br>② 在 webpack.config.js 配置文件中，添加 vue-loader 的配置项如下：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">const</span> VueLoaderPlugin <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'vue-loader/lib/plugin'</span><span class="token punctuation">)</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    module<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    rules<span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token comment" spellcheck="true">// ... 其它规则</span>
        <span class="token punctuation">{</span> test<span class="token punctuation">:</span> <span class="token regex">/\.vue$/</span><span class="token punctuation">,</span> loader<span class="token punctuation">:</span> <span class="token string">'vue-loader'</span> <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token comment" spellcheck="true">// ... 其它插件</span>
    <span class="token keyword">new</span> <span class="token class-name">VueLoaderPlugin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 请确保引入这个插件！</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="3-4-在-webpack-项目中使用-vue"><a href="#3-4-在-webpack-项目中使用-vue" class="headerlink" title="3.4 在 webpack 项目中使用 vue"></a>3.4 在 webpack 项目中使用 vue</h3><p>① 运行 npm i vue –S 安装 vue<br>② 在 src -&gt; index.js 入口文件中，通过 import Vue from ‘vue’ 来导入 vue 构造函数<br>③ 创建 vue 的实例对象，并指定要控制的 el 区域<br>④ 通过 render 函数渲染 App 根组件</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 1. 导入 Vue 构造函数</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token comment" spellcheck="true">// 2. 导入 App 根组件</span>
<span class="token keyword">import</span> App <span class="token keyword">from</span> <span class="token string">'./components/App.vue'</span>
<span class="token keyword">const</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// 3. 指定 vm 实例要控制的页面区域</span>
    el<span class="token punctuation">:</span> <span class="token string">'#app'</span><span class="token punctuation">,</span>
    <span class="token comment" spellcheck="true">// 4. 通过 render 函数，把指定的组件渲染到 el 区域中</span>
    render<span class="token punctuation">:</span> h <span class="token operator">=</span><span class="token operator">></span> <span class="token function">h</span><span class="token punctuation">(</span>App<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
<h3 id="3-5-webpack-打包发布"><a href="#3-5-webpack-打包发布" class="headerlink" title="3.5 webpack 打包发布"></a>3.5 webpack 打包发布</h3><p>上线之前需要通过webpack将应用进行整体打包，可以通过 package.json 文件配置打包命令：</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 在package.json文件中配置 webpack 打包命令</span>
<span class="token comment" spellcheck="true">// 该命令默认加载项目根目录中的 webpack.config.js 配置文件</span>
<span class="token string">"scripts"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// 用于打包的命令</span>
    <span class="token string">"build"</span><span class="token punctuation">:</span> <span class="token string">"webpack -p"</span><span class="token punctuation">,</span>
    <span class="token comment" spellcheck="true">// 用于开发调试的命令</span>
    <span class="token string">"dev"</span><span class="token punctuation">:</span> <span class="token string">"webpack-dev-server --open --host 127.0.0.1 --port 3000"</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span></code></pre>
<h2 id="4-Vue-脚手架"><a href="#4-Vue-脚手架" class="headerlink" title="4. Vue 脚手架"></a>4. Vue 脚手架</h2><h3 id="4-1-Vue-脚手架的基本用法"><a href="#4-1-Vue-脚手架的基本用法" class="headerlink" title="4.1 Vue 脚手架的基本用法"></a>4.1 Vue 脚手架的基本用法</h3><p>Vue 脚手架用于快速生成 Vue 项目基础架构，其官网地址为： <a href="https://cli.vuejs.org/zh/" target="_blank" rel="noopener">https://cli.vuejs.org/zh/</a></p>
<p>使用步骤</p>
<ol>
<li>安装 3.x 版本的 Vue 脚手架：</li>
</ol>
<p>npm install -g @vue/cli</p>
<p>基于3.x版本的脚手架创建vue项目</p>
<p>// 1. 基于 交互式命令行 的方式，创建 新版 vue 项目<br>vue create my-project<br>// 2. 基于 图形化界面 的方式，创建 新版 vue 项目<br>vue ui<br>// 3. 基于 2.x 的旧模板，创建 旧版 vue 项目<br>npm install -g @vue/cli-init<br>vue init webpack my-project</p>
<h3 id="4-2-Vue-脚手架生成的项目结构分析"><a href="#4-2-Vue-脚手架生成的项目结构分析" class="headerlink" title="4.2 Vue 脚手架生成的项目结构分析"></a>4.2 Vue 脚手架生成的项目结构分析</h3><img width="80%" src="./images/04.png">

<h3 id="4-3-Vue-脚手架的自定义配置"><a href="#4-3-Vue-脚手架的自定义配置" class="headerlink" title="4.3 Vue 脚手架的自定义配置"></a>4.3 Vue 脚手架的自定义配置</h3><ol>
<li><p>通过 package.json 配置项目</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 必须是符合规范的json语法</span>
<span class="token string">"vue"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
 <span class="token string">"devServer"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
     <span class="token string">"port"</span><span class="token punctuation">:</span> <span class="token string">"8888"</span><span class="token punctuation">,</span>
     <span class="token string">"open"</span> <span class="token punctuation">:</span> <span class="token boolean">true</span>
 <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span></code></pre>
<p>注意： 不推荐使用这种配置方式。因为 package.json 主要用来管理包的配置信息；为了方便维护，推荐将 vue 脚<br>手架相关的配置，单独定义到 vue.config.js 配置文件中。</p>
</li>
<li><p>通过单独的配置文件</p>
</li>
</ol>
<p>① 在项目的跟目录创建文件 vue.config.js<br>② 在该文件中进行相关配置，从而覆盖默认配置</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// vue.config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    devServer<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    port<span class="token punctuation">:</span> <span class="token number">8888</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="5-Element-UI-的基本使用"><a href="#5-Element-UI-的基本使用" class="headerlink" title="5. Element-UI 的基本使用"></a>5. Element-UI 的基本使用</h2><p><code>Element-UI</code>： 一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的<code>桌面端组件库</code>。<br>官网地址为： <a href="http://element-cn.eleme.io/#/zh-CN" target="_blank" rel="noopener">http://element-cn.eleme.io/#/zh-CN</a></p>
<h3 id="5-1-基于命令行方式手动安装"><a href="#5-1-基于命令行方式手动安装" class="headerlink" title="5.1 基于命令行方式手动安装"></a>5.1 基于命令行方式手动安装</h3><p>① 安装依赖包 npm i element-ui –S<br>② 导入 Element-UI 相关资源</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 导入组件库</span>
<span class="token keyword">import</span> ElementUI <span class="token keyword">from</span> <span class="token string">'element-ui'</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 导入组件相关样式</span>
<span class="token keyword">import</span> <span class="token string">'element-ui/lib/theme-chalk/index.css'</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 配置 Vue 插件</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>ElementUI<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<h3 id="5-2-基于图形化界面自动安装"><a href="#5-2-基于图形化界面自动安装" class="headerlink" title="5.2 基于图形化界面自动安装"></a>5.2 基于图形化界面自动安装</h3><p>① 运行 vue ui 命令，打开图形化界面<br>② 通过 Vue 项目管理器， 进入具体的项目配置面板<br>③ 点击 插件 -&gt; 添加插件，进入插件查询面板<br>④ 搜索 vue-cli-plugin-element 并安装<br>⑤ 配置插件，实现按需导入，从而减少打包后项目的体积</p>
<script>
        document.querySelectorAll('.github-emoji')
          .forEach(el => {
            if (!el.dataset.src) { return; }
            const img = document.createElement('img');
            img.style = 'display:none !important;';
            img.src = el.dataset.src;
            img.addEventListener('error', () => {
              img.remove();
              el.style.color = 'inherit';
              el.style.backgroundImage = 'none';
              el.style.background = 'none';
            });
            img.addEventListener('load', () => {
              img.remove();
            });
            document.body.appendChild(img);
          });
      </script>
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://shenzhaoliang.gitee.io" rel="external nofollow noreferrer">小沈</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://shenzhaoliang.gitee.io/xiaoshensay/2019/06/15/webengineering/">https://shenzhaoliang.gitee.io/xiaoshensay/2019/06/15/webengineering/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://shenzhaoliang.gitee.io" target="_blank">小沈</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/xiaoshensay/tags/Vue/">
                                    <span class="chip bg-color">Vue</span>
                                </a>
                            
                                <a href="/xiaoshensay/tags/webpack/">
                                    <span class="chip bg-color">webpack</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            
        </div>
    </div>

    
        <link rel="stylesheet" href="/libs/gitalk/gitalk.css">
<link rel="stylesheet" href="/css/my-gitalk.css">

<div class="card gitalk-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="gitalk-container" class="card-content"></div>
</div>

<script src="/libs/gitalk/gitalk.min.js"></script>
<script>
    let gitalk = new Gitalk({
        clientID: '',
        clientSecret: '',
        repo: '',
        owner: '',
        admin: null,
        id: '2019-06-15T17-32-22',
        distractionFreeMode: false  // Facebook-like distraction free mode
    });

    gitalk.render('gitalk-container');
</script>
    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/xiaoshensay/2019/06/23/git/">
                    <div class="card-image">
                        
                        <img src="/medias/featureimages/100.jpg" class="responsive-img" alt="Git">
                        
                        <span class="card-title">Git</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Git是一个版本管理控制系统（缩写VCS），它可以在任何时间点，将文档的状态作为更新记录保存起来，也可以在任何时间点，将更新记录恢复回来。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2019-06-23
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/xiaoshensay/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA/" class="post-category">
                                    计算机
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/xiaoshensay/tags/%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86/">
                        <span class="chip bg-color">版本管理</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/xiaoshensay/2019/06/09/ajax/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/16.jpg" class="responsive-img" alt="Ajax">
                        
                        <span class="card-title">Ajax</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Ajax是指一种创建交互式、快速动态网页应用的网页开发技术，无需重新加载整个网页的情况下，能够更新部分网页的技术。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2019-06-09
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/xiaoshensay/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA/" class="post-category">
                                    计算机
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/xiaoshensay/tags/Ajax/">
                        <span class="chip bg-color">Ajax</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('3'),
            headingSelector: 'h1, h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="955411580"
                   fixed='true'
                   autoplay='false'
                   theme:='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2019</span>
            <a href="https://shenzhaoliang.gitee.io" target="_blank">小沈</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">31.5k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/stszl" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:stszl@sina.cn" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=409178030" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 409178030" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/xiaoshensay/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/xiaoshensay/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
